package app.backend.controller;

import app.backend.common.CommonResult;
import app.backend.entity.AppClientUser;
import app.backend.service.AppClientUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import app.backend.utils.SessionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Map;

/**
 * @description: 用户接口
 * @author: Amos
 * @create_time: 2022/8/16 15:25
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private AppClientUserService clientUserService;

    @PostMapping("/getUserInfo")
    public CommonResult<AppClientUser> getUserInfo() {
        String userId = SessionUtils.getCurrentUserId();
        if(userId == null){
            log.info("当前用户id不能为空");
            return null;
        }
        AppClientUser clientUser = clientUserService.getUserInfo(userId);
        clientUser.setToken(SessionUtils.saveSessionInfo(clientUser));
        return CommonResult.data(clientUser);
    }



    @PostMapping("/login")
    public CommonResult<AppClientUser> login(@RequestBody Map<String,Object> map) {
        String jsCode = (String) map.get("jsCode");
        String code = (String) map.get("code");
        AppClientUser clientUser = clientUserService.login(code,jsCode);
        if (clientUser != null) {
            clientUser.setToken(SessionUtils.saveSessionInfo(clientUser));
            return CommonResult.data(clientUser);
        }
        return null;
    }



    @PostMapping("/quit")
    public void userQuit(@RequestParam("code")String code) {
        AppClientUser clientUser = clientUserService.getUser(code);
        SessionUtils.deleteSessionInfo(clientUser);
    }

    @PostMapping("/updateClientUser")
    public CommonResult<AppClientUser> updateClientUser(@RequestBody AppClientUser registerInfo) {
        return CommonResult.data(clientUserService.updateClientUser(registerInfo));
    }
}

